package view;

import controller.BlogController;
import controller.UserController;
import entity.Blog;
import util.UserSessionManager;

import java.util.Scanner;

public class UserView {
    private UserController userController;

    public void setUserController(UserController userController) {
        this.userController = userController;
    }

    private BlogController blogController;

    public void setBlogController(BlogController blogController) {
        this.blogController = blogController;
    }

    Scanner scanner = new Scanner(System.in);

    public void showUser() {
        while (true) {
            System.out.println("=== 用户主页 ===");
            System.out.println("1. 查看热榜博文");
            System.out.println("2. 搜索功能");
            System.out.println("3. 个人中心");
            System.out.println("4. 退出登录");
            System.out.print("请选择操作: ");

            int choice = scanner.nextInt();
            scanner.nextLine(); // 清空换行符

            switch (choice) {
                case 1:
                    showHotBlogs();
                    break;
                case 2:
                    searchBlogs();
                    break;
                case 3:
                    int userId = UserSessionManager.getInstance().getCurrentUser().getId();
                    showPersonalCenter(userId);
                    break;
                case 4:
                    System.out.println("退出成功！");
                    userController.logout();
                    return;
                default:
                    System.out.println("无效选项，请重新输入！");
                    break;
            }
        }
    }

    private void showHotBlogs() {
        while (true) {
            System.out.println("=== 文章热榜 ===");
            System.out.println("1. 按点赞数排序");
            System.out.println("2. 按评论数排序");
            System.out.println("3. 综合排序");
            System.out.println("4. 返回");
            System.out.print("请选择排序方式：");

            int choice = scanner.nextInt();
            scanner.nextLine();
            switch (choice) {
                case 1:
                    blogController.showHotBlogs("likeCount");
                    break;
                case 2:
                    blogController.showHotBlogs("commentCount");
                    break;
                case 3:
                    blogController.showHotBlogs("viewCount");
                    break;
                case 4:
                    System.out.println("返回");
                    return;
                default:
                    System.out.println("无效选项，请重新输入！");
                    break;
            }
        }
    }

    private void searchBlogs() {
        while (true) {
            System.out.println("=== 搜索 ===");
            System.out.println("1. 搜索文章");
            System.out.println("2. 搜索博主");
            System.out.println("3. 搜索专栏");
            System.out.print("请选择操作(输入0返回)：");

            int choice = scanner.nextInt();
            scanner.nextLine();
            if(choice == 0) return;

            System.out.print("请输入关键字：");
            String keyword = scanner.nextLine();

            if (keyword == null || keyword.trim().isEmpty()) {
                System.out.println("请输入有效的关键字！");
                continue;
            }

            switch (choice) {
                case 1:
                    blogController.searchBlogsByKeyword(keyword);
                    break;
                case 2:
                    blogController.searchAuthorsByKeyword(keyword);
                    break;
                case 3:
                    blogController.searchColumnsByKeyword(keyword);
                    break;
                default:
                    System.out.println("无效选项，请重新输入！");
            }
        }
    }

    private void showPersonalCenter(int userId) {
        while (true) {
            System.out.println("=== 个人中心 ===");
            System.out.println("1. 我的专栏");
            System.out.println("2. 我的博文");
            System.out.println("3. 我的关注");
            System.out.println("4. 我赞过的");
            System.out.println("5. 查看个人信息");
            System.out.println("6. 返回");
            System.out.print("请选择：");

            int choice = scanner.nextInt();
            switch (choice) {
                case 1:
                    System.out.println("我的专栏：");
                    blogController.showMyColumns();
                    showColumnsOptions();
                    break;
                case 2:
                    System.out.println("我的博文：");
                    blogController.showMyBlogs();
                    showMyBlogsOptions();
                    break;
                case 3:
                    System.out.println("我的关注：");
                    blogController.getFolloweeUsers();
                    break;
                case 4:
                    System.out.println("我的点赞过的博文列表：");
                    blogController.showLikedBlogs();
                    break;
                case 5:
                    userController.viewProfile(userId);
                    break;
                case 6:
                    return;
                default:
                    System.out.println("无效选项，请重新输入！");
            }
        }
    }

    public void showOptions(int userId, int blogId, int authorId) {
        while (true) {
            if (blogController.isFollowing(userId, authorId)) {
                System.out.print("1. 取关博主 ");
            } else {
                System.out.print("1. 关注博主 ");
            }
            if (blogController.isUserLikedBlog(userId, blogId)) {
                System.out.print("2. 取消点赞文章 ");
            } else {
                System.out.print("2. 点赞文章 ");
            }
            System.out.print("3. 点赞/取消点赞评论 ");
            System.out.print("4. 评论博文 ");
            System.out.print("5. 删除评论 ");
            System.out.print("6. 更换评论排序方式 ");
            System.out.print("7. 举报博文 ");
            System.out.println("8. 返回");

            System.out.println("请选择操作：");
            int choice = scanner.nextInt();
            scanner.nextLine();

            switch (choice) {
                case 1:
                    blogController.toggleFollowAuthor(userId, authorId);
                    break;
                case 2:
                    blogController.toggleLikeBlog(userId, blogId);
                    break;
                case 3:
                    blogController.toggleComment(userId, blogId);
                    break;
                case 4:
                    blogController.commentOnBlog(blogId, userId);
                    break;
                case 5:
                    blogController.deleteComment(blogId);
                    break;
                case 6:
                    blogController.sortComments(blogId);
                    break;
                case 7:
                    blogController.reportBlog(blogId);
                    break;
                case 8:
                    return;
                default:
                    System.out.println("无效选项，请重新输入！");
            }
            Blog blog = blogController.getBlogById(blogId);
            blogController.showBlogDetails(blog);
        }
    }

    private void showColumnsOptions(){
        while (true) {
            System.out.println("1. 新增专栏");
            System.out.println("2. 添加文章进入专栏");
            System.out.println("3. 删除专栏");
            System.out.println("4. 取消/置顶专栏中的博文");
            System.out.println("5. 将文章移出专栏");
            System.out.println("6. 切换专栏文章排序");
            System.out.println("7. 返回");

            System.out.println("请选择操作：");
            int choice = scanner.nextInt();
            scanner.nextLine();

            switch (choice) {
                case 1:
                    blogController.createColumn();
                    break;
                case 2:
                    blogController.addBlogToMyColumn();
                    break;
                case 3:
                    blogController.deleteColumn();
                    break;
                case 4:
                    blogController.toggleTopStatus();
                    break;
                case 5:
                    blogController.removeMyBlog();
                    break;
                case 6:
                    blogController.toggleColumnBlogSortOrder();
                    break;
                case 7:
                    return;
                default:
                    System.out.println("无效选项，请重新输入！");
                    break;
            }
            blogController.showMyColumns();
        }
    }

    private void showMyBlogsOptions() {
        while (true) {
            System.out.println("1. 发布博文");
            System.out.println("2. 修改博文");
            System.out.println("3. 删除博文");
            System.out.println("4. 查看博文");
            System.out.println("5. 删除评论");
            System.out.println("6. 返回");

            System.out.println("请选择操作：");
            int choice = scanner.nextInt();
            scanner.nextLine();

            switch (choice) {
                case 1:
                    blogController.createBlog();
                    break;
                case 2:
                    blogController.updateBlog();
                    break;
                case 3:
                    blogController.deleteBlog();
                    break;
                case 4:
                    blogController.showMyBlogDetails();
                    break;
                case 5:
                    blogController.deleteCommentOnMyBlog();
                    break;
                case 6:
                    return;
                default:
                    System.out.println("无效选项，请重新输入！");
            }
            blogController.showMyBlogs();
        }
    }
}